﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DstryByContact : MonoBehaviour {

	public GameObject explosion;
	public GameObject playerExplosion;

	public int scoreValue;
	private GameController gameController; // Every instance of asteroid has to find a reference to it after the game starts

	void Start() 
	{
		GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
		if (gameControllerObject != null) 
		{
			gameController = gameControllerObject.GetComponent<GameController> ();
		}
		if (gameController == null) {
			Debug.Log ("Cannot find 'gameController' object");
		}
	}
	void OnTriggerEnter(Collider other) {
		if (other.tag == "Boundary")	// prevent both boundary and asteroid from being destroyed on the first frame
			return;
		Instantiate (explosion, transform.position, transform.rotation);
		if (other.tag == "Player") {
			Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
//			GameController.gameOver (); // Player's spaceship has been destroyed, game over
		}
		gameController.addScore (scoreValue);
		Destroy(other.gameObject); // Only tag the gameobject 'to be destroyed' 
		Destroy(gameObject);		  // All game objects are destroyed at the end of the frame
	}
}
